﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Security;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FredCK.FCKeditorV2;
//using KimSoft;
using BLL;
using DTO;
using System.IO;

public partial class AddEditAlbum : BasePage
    {
        string dirUrl = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            SetPropertyFileUpload();
            if (!this.IsPostBack)
            {
                // if a ID param is present on the querystring, switch to Edit mode for that article,
                // but only after checking that the current user is an Administrator or an Editor
                if (!string.IsNullOrEmpty(this.Request.QueryString["ID"]))
                {
                    

                    if (this.User.Identity.IsAuthenticated &&
                       (this.User.IsInRole("Administrators") || this.User.IsInRole("Editors")))
                    {
                        dvwArticle.ChangeMode(DetailsViewMode.Edit);
                        UpdateTitle();
                    }
                    else
                        throw new SecurityException("You are not allowed to edit existent articles!");
                }
            }
        }
        private void SetPropertyFileUpload()
        {
            FileUploadResize1.width = 200;
            FileUploadResize1.height = 200;
            FileUploadResize1.FolderUpload = "Thuvienhinhanh";
            Control ctl = dvwArticle.FindControl("txtimageUrl");
            if (ctl != null)
            {
                TextBox txtImageUrl = ctl as TextBox;
                FileUploadResize1.ObjectControl = txtImageUrl;
            }
        }
        protected void dvwArticle_DataBound(object sender, EventArgs e)
        {
        }

        protected void dvwArticle_ModeChanged(object sender, EventArgs e)
        {
            UpdateTitle();
        }

        private void UpdateTitle()
        {
            lblNewArticle.Visible = (dvwArticle.CurrentMode == DetailsViewMode.Insert);
            lblEditArticle.Visible = !lblNewArticle.Visible;
        }
        //protected void btnUpload_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        //create folder and path
        //        int id = int.Parse(this.Request.QueryString["ID"]);
        //        string url = Globals.ApplicationPath;
        //        if (!url.EndsWith("/"))
        //            url = url + "/";
        //        dirUrl = url + "Upload/Thuvienhinhanh/" + id+"/";
        //        string dirPath = Server.MapPath(dirUrl);
        //        if (!Directory.Exists(dirPath))
        //            Directory.CreateDirectory(dirPath);
        //        (dvwArticle.FindControl("txtBody") as TextBox).Text = dirUrl;
        //        // Get the HttpFileCollection
        //        HttpFileCollection hfc = Request.Files;
        //        for (int i = 0; i < hfc.Count; i++)
        //        {
        //            HttpPostedFile hpf = hfc[i];
        //            if (hpf.ContentLength > 0)
        //            {
        //                hpf.SaveAs(dirPath + System.IO.Path.GetFileName(hpf.FileName));
        //                Literal1.Text = "</br><b>Tên file: </b>" + hpf.FileName + "  <b>kích thước:</b> " +
        //                    hpf.ContentLength + "  <b>Loại:</b> " + hpf.ContentType + " <b>Upload thành công. </b><br/>";
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //    }
        //}       
    }